
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <title>k8s删除Terminating状态的命名空间</title>
            </head>
            <body>
            <a href="https://andyoung.blog.csdn.net">原作者博客</a>
            <div id="content_views" class="markdown_views prism-atom-one-light">
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                    <blockquote> 
 <p>我们都知道在k8s中<code>namespace</code>有两种常见的状态，即Active和Terminating状态，其中后者一般会比较少见，只有当对应的命名空间下还存在运行的资源，但是该命名空间被删除时才会出现所谓的<code>terminating</code>状态，这种情况下只要等待k8s本身将命名空间下的资源回收后，该命名空间将会被系统自动删除。但是今天遇到命名空间下已没相关资源，但依然无法删除<code>terminating</code>状态的命名空间的情况，特此记录一下.</p> 
</blockquote> 
<h4><a id="_2"></a><strong>查看命名空间详情</strong></h4> 
<pre><code>$ kubectl  get ns  | grep rdb
rdbms                  Terminating   6d21h

$ kubectl  get ns rdbms -o yaml
apiVersion: v1
kind: Namespace
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"rdbms"}}
  creationTimestamp: "2019-10-14T12:17:44Z"
  deletionTimestamp: "2019-10-14T12:30:27Z"
  name: rdbms
  resourceVersion: "8844754"
  selfLink: /api/v1/namespaces/rdbms
  uid: 29067ddf-56d7-4cce-afa3-1fbdbb221ab1
spec:
  finalizers:
  - kubernetes
status:
  phase: Terminating
</code></pre> 
<h4><a id="_28"></a><strong>查看该命名空间下的资源</strong></h4> 
<p>查看k8s集群中可以使用命名空间隔离的资源</p> 
<pre><code>
kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n rdbms 
</code></pre> 
<p>发现rdbms命名空间下并无资源占用</p> 
<h3><a id="_41"></a><strong>尝试对命名空间进行删除</strong></h3> 
<pre><code># 直接删除命名空间rdbms
## 提示删除操作未能完成，说系统会在确定没用没用资源后将会被自动删除
$ kubectl  delete ns rdbms
Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.


# 使用强制删除(依然无法删除该命名空间)
$ kubectl  delete ns rdbms --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
Error from server (Conflict): Operation cannot be fulfilled on namespaces "rdbms": The system is ensuring all content is removed from this namespace.  Upon completion, this namespace will automatically be purged by the system.
</code></pre> 
<h3><a id="_56"></a><strong>使用原生接口删除</strong></h3> 
<pre><code># 获取namespace的详情信息
$ kubectl  get ns rdbms  -o json &gt; rdbms.json

# 查看napespace定义的json配置
## 删除掉spec部分即可
$ cat rdbms.json
{
    "apiVersion": "v1",
    "kind": "Namespace",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n"
        },
        "creationTimestamp": "2019-10-14T12:17:44Z",
        "deletionTimestamp": "2019-10-14T12:30:27Z",
        "name": "rdbms",
        "resourceVersion": "8844754",
        "selfLink": "/api/v1/namespaces/rdbms",
        "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1"
    },
    "spec": {
        "finalizers": [
            "kubernetes"
        ]
    },
    "status": {
        "phase": "Terminating"
    }
}

# 使用http接口进行删除
$ curl -k -H "Content-Type:application/json" -X PUT --data-binary @rdbms.json https://x.x.x.x:6443/api/v1/namespaces/rdbms/finalize
{
  "kind": "Namespace",
  "apiVersion": "v1",
  "metadata": {
    "name": "rdbms",
    "selfLink": "/api/v1/namespaces/rdbms/finalize",
    "uid": "29067ddf-56d7-4cce-afa3-1fbdbb221ab1",
    "resourceVersion": "8844754",
    "creationTimestamp": "2019-10-14T12:17:44Z",
    "deletionTimestamp": "2019-10-14T12:30:27Z",
    "annotations": {
      "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"name\":\"rdbms\"}}\n"
    }
  },
  "spec": {

  },
  "status": {
    "phase": "Terminating"
  }

# 再次查看namespace发现已经被删除了
$ kubectl  get ns  | grep rdb
</code></pre>
                </div>
            </body>
            </html>
            